/*
 * ShipUI.java
 *
 * Created on 24-Jun-2010, 09:51:24
 */

package camp.anchors;

import camp.anchors.Anchor;
import campskeleton.CampSkeleton;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import utils.ListDownLayout;
import utils.WeakListener.Changed;

/**
 *
 * @author twak
 */
public class ShipUI extends javax.swing.JPanel {

    Ship.Instance instance;
    int instanceNo;
    Ship ship;
    FeatureUI parent;

    /** Creates new form ShipUI */
    public ShipUI() {
        initComponents();
    }
    public ShipUI( Ship ship, FeatureUI parent ) {
        this.ship = ship;
        this.parent = parent;
        initComponents();
        initList();
        setInstanceNo(0);

        CampSkeleton.instance.selectedAnchorListeners.add(new Changed() {

            @Override
            public void changed() {
                updateHighlights();
            }
        });

        nameField.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                instance.name = nameField.getText();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                instance.name = nameField.getText();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                instance.name = nameField.getText();
            }
        });
    }

    void initList()
    {
        anchorList.removeAll();
        
        if (ship.getInstances().size() == 0)
            return;
        
        instance = ship.getInstances().get(instanceNo);

        nameField.setText(instance.name);
        
        anchorList.setLayout(new ListDownLayout());
        final ButtonGroup anchorBG = new ButtonGroup();
        final JToggleButton dummy = new JToggleButton("none");
        anchorBG.add(dummy);
        CampSkeleton.instance.selectedAnchorListeners.add(new Changed() {

            @Override
            public void changed() {
                if (CampSkeleton.instance.selectedAnchor == null)
                    dummy.setSelected(true);
            }
        });

        for (Anchor a : instance.anchors)
            anchorList.add( a.createUI(anchorBG) );

        anchorBG.setSelected(dummy.getModel(), true);

//        SwingUtilities.invokeLater(new Runnable() {
//            @Override
//            public void run()
//            {
//                ((AnchorUI)anchorList.getComponent(0)).getButton().doClick();
//            }
//        });

        updateHighlights();
        anchorList.revalidate();
    }

    private void updateHighlights()
    {
        List<Anchor> highlit = new ArrayList();
        if (instance != null)
        for ( Anchor ac : instance.anchors)
            highlit.add(ac);
        CampSkeleton.instance.highlightFor(highlit);
    }

    private void setInstanceNo( int val )
    {
        CampSkeleton.instance.nowSelectingFor(null);
        CampSkeleton.instance.highlightFor(new ArrayList<Anchor>());

        instanceNo = val;
        if (instanceNo >= ship.getInstances().size())
            instanceNo = 0;

        if (instanceNo < 0) 
            instanceNo = ship.getInstances().size() - 1;

        pageLabel.setText((instanceNo + 1) + "/" + ship.getInstances().size());

        initList();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nameField = new javax.swing.JTextField();
        rightButton = new javax.swing.JButton();
        leftButton = new javax.swing.JButton();
        neuButton = new javax.swing.JButton();
        killButton = new javax.swing.JButton();
        pageLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        anchorList = new javax.swing.JPanel();

        nameField.setText("ship instance name");
        nameField.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                nameFieldInputMethodTextChanged(evt);
            }
        });
        nameField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                nameFieldPropertyChange(evt);
            }
        });

        rightButton.setText(">");
        rightButton.setToolTipText("next");
        rightButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rightButtonActionPerformed(evt);
            }
        });

        leftButton.setText("<");
        leftButton.setToolTipText("previous");
        leftButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                leftButtonActionPerformed(evt);
            }
        });

        neuButton.setText("+");
        neuButton.setToolTipText("new instance");
        neuButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                neuButtonActionPerformed(evt);
            }
        });

        killButton.setText("-");
        killButton.setToolTipText("delete current instance");
        killButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                killButtonActionPerformed(evt);
            }
        });

        pageLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        pageLabel.setText("1/1");

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        javax.swing.GroupLayout anchorListLayout = new javax.swing.GroupLayout(anchorList);
        anchorList.setLayout(anchorListLayout);
        anchorListLayout.setHorizontalGroup(
            anchorListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 302, Short.MAX_VALUE)
        );
        anchorListLayout.setVerticalGroup(
            anchorListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 438, Short.MAX_VALUE)
        );

        jScrollPane2.setViewportView(anchorList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(neuButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(killButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 113, Short.MAX_VALUE)
                .addComponent(leftButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rightButton)
                .addGap(9, 9, 9))
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(neuButton)
                    .addComponent(killButton)
                    .addComponent(rightButton)
                    .addComponent(leftButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 440, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void nameFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_nameFieldPropertyChange
//        instance.name = nameField.getText();
    }//GEN-LAST:event_nameFieldPropertyChange

    private void rightButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rightButtonActionPerformed
        setInstanceNo(instanceNo + 1);
    }//GEN-LAST:event_rightButtonActionPerformed

    private void leftButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_leftButtonActionPerformed
        setInstanceNo(instanceNo - 1);
    }//GEN-LAST:event_leftButtonActionPerformed

    private void neuButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_neuButtonActionPerformed
        ship.newInstance();
        setInstanceNo( ship.getInstances().size()-1 );

        initList();
    }//GEN-LAST:event_neuButtonActionPerformed

    private void killButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_killButtonActionPerformed
        ship.removeInstance(instanceNo);

        setInstanceNo(instanceNo);

        if (ship.getInstances().size() == 0)
        {
            parent.removeShip(ship);
            CampSkeleton.instance.showRoot();
        }



        initList();
    }//GEN-LAST:event_killButtonActionPerformed

    private void nameFieldInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_nameFieldInputMethodTextChanged
//        instance.name = nameField.getText();
    }//GEN-LAST:event_nameFieldInputMethodTextChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel anchorList;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton killButton;
    private javax.swing.JButton leftButton;
    private javax.swing.JTextField nameField;
    private javax.swing.JButton neuButton;
    private javax.swing.JLabel pageLabel;
    private javax.swing.JButton rightButton;
    // End of variables declaration//GEN-END:variables

}
